﻿using Microsoft.OpenApi.Models;
using System.Reflection;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using LJX.SmartPark.Common.Const;

namespace LJX.SmartPark.WebApi.Extensions
{
    public static class SwaggerServiceRegister
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceCollection"></param>
        /// <returns></returns>
        public static IServiceCollection SwaggerRegister(this IServiceCollection serviceCollection)
        {
            serviceCollection.AddSwaggerGen(option =>
            {
                option.SwaggerDoc("SD", new OpenApiInfo { Version = "v1", Title = "神雕侠侣", Description = "神雕侠侣" });


                option.SwaggerDoc("IotInfo", new OpenApiInfo { Version = "v1", Title = "物联网设备", Description = "物联网设备" });
                option.SwaggerDoc("Area", new OpenApiInfo { Version = "v1", Title = "区域设置", Description = "区域设置" });
                option.SwaggerDoc("SysManage", new OpenApiInfo { Version = "v1", Title = "系统管理", Description = "系统管理" });
                option.SwaggerDoc("LogManage", new OpenApiInfo { Version = "v1", Title = "日志管理", Description = "日志管理" });
                option.SwaggerDoc("SysTools", new OpenApiInfo { Version = "v1", Title = "系统工具", Description = "系统工具" });
                option.SwaggerDoc("MonitorData", new OpenApiInfo { Version = "v1", Title = "监控数据", Description = "监控数据" });
                option.SwaggerDoc("SafeManage", new OpenApiInfo { Version = "v1", Title = "安全管理", Description = "安全管理" });
                option.SwaggerDoc("EnergyManage", new OpenApiInfo { Version = "v1", Title = "能耗管理", Description = "能耗管理" });
                option.SwaggerDoc("AssetManage", new OpenApiInfo { Version = "v1", Title = "资产管理", Description = "资产管理" });
                option.SwaggerDoc("ParkManage", new OpenApiInfo { Version = "v1", Title = "园区管理", Description = "园区管理" });

                //如果需要显示控制器注释只需将第二个参数设置为true
                var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                option.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename), true);

                option.AddSecurityDefinition(JwtBearerDefaults.AuthenticationScheme, new OpenApiSecurityScheme()
                {
                    Description = "需要输入token",
                    Name = AssemblyConst.AUTHORIZATION,
                    In = ParameterLocation.Header,
                    Type = SecuritySchemeType.ApiKey,
                    Scheme = JwtBearerDefaults.AuthenticationScheme
                });
                option.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                      {
                          new OpenApiSecurityScheme {
                            Reference = new OpenApiReference{
                                Type = ReferenceType.SecurityScheme,
                                Id = JwtBearerDefaults.AuthenticationScheme
                            }
                        },
                        new string[]{}
                      }

                });
            });

            return serviceCollection;
        }
    }
}
